package cn.kimoon.dau_android;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kaopiz.kprogresshud.KProgressHUD;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import cn.kimoon.dau_android.API.ApiDAU;
import cn.kimoon.dau_android.API.AppClient;
import cn.kimoon.dau_android.Dialog.DialogManager;
import cn.kimoon.dau_android.Dialog.LoadingDialog;
import cn.kimoon.dau_android.Model.ModelCollectorInfo;
import cn.kimoon.dau_android.Model.ModelContactItem;
import cn.kimoon.dau_android.Model.ModelLogin;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by xiangdong on 2016/11/28.
 */

public class ControlAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
    private ModelCollectorInfo info;
    private Context mContext;

    public ControlAdapter(ModelCollectorInfo info, Context mContext) {
        this.info = info;
        this.mContext = mContext;
    }

    public void setInfo(ModelCollectorInfo info) {
        this.info = info;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return info == null ? 0 : info.control_contact.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_control, parent, false);
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.control_text_view);
            holder.mSwitch = (Switch) convertView.findViewById(R.id.control_switch);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ModelContactItem item = info.control_contact.get(position);
        holder.nameTextView.setText(item.name);
        holder.mSwitch.setChecked(item.value == 1);
        holder.mSwitch.setOnCheckedChangeListener(this);
        holder.mSwitch.setTag(position);

        return convertView;
    }

    static class ViewHolder {
        TextView nameTextView;
        Switch mSwitch;
    }

    // 按键处理
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.control_switch:
                int position = (int) buttonView.getTag();
                requestSetContact(position);
                break;
        }
    }

    // 网络请求
    private void requestSetContact(int index) {
//        DialogManager.getInstance().showLoadingDialog(mContext, "");
        final KProgressHUD hud = KProgressHUD.create(mContext, KProgressHUD.Style.SPIN_INDETERMINATE).show();

        final ModelContactItem item = info.control_contact.get(index);
        item.value = (item.value == 0 ? 1 : 0);

        Map<String, Object> map = new HashMap<>();
        map.put("id", info.id);
        map.put("control_contact", info.getControlContactRequestArray());

        ApiDAU apiDAU = AppClient.retrofit().create(ApiDAU.class);
        Call<ModelLogin> call = apiDAU.apiContact(map);
        call.enqueue(new Callback<ModelLogin>() {
            @Override
            public void onResponse(Call<ModelLogin> call, Response<ModelLogin> response) {
                hud.dismiss();
                if (response.body() == null || response.body().errCode != 200) {
                    item.value = (item.value == 0 ? 1 : 0);
                    notifyDataSetChanged();
                    String msg = (response.body() == null ? response.message() : response.body().msg);
                    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
//                    DialogManager.getInstance().showToastDialog(mContext, LoadingDialog.LoadingState.STATE_FAIL, response.body().msg);
                }
            }

            @Override
            public void onFailure(Call<ModelLogin> call, Throwable t) {
                System.out.println("网络请求失败");
                item.value = (item.value == 0 ? 1 : 0);
                notifyDataSetChanged();
                Toast.makeText(mContext, R.string.internet_request_failed, Toast.LENGTH_SHORT).show();
//                DialogManager.getInstance().showToastDialog(mContext, LoadingDialog.LoadingState.STATE_FAIL, "网络请求失败");
            }
        });
    }
}
